package com.test.workdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.workdemo.dao.CeshiTableMapper;
import com.test.workdemo.pojo.entity.CeshiTable;
import com.test.workdemo.service.AsyncDemoService;
import com.test.workdemo.service.CeshiTableService;
import com.test.workdemo.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 测试(ceshiTable)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/ceshiTable")
@Api(tags = "数据测试")
public class CeshiTableController {
    /**
     * 服务对象
     */
    @Autowired
    private AsyncDemoService asyncDemoService;

    @Autowired
    private CeshiTableService ceshiTableService;

    @Autowired
    private CeshiTableMapper ceshiTableMapper;

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @ApiOperation("根据ID查询商品")
    public JsonResult selectOne() {
        //CeshiTable ceshi = ceshiTableService.getById(10);
        //Long getage = ceshiTableMapper.getage(90);
        return JsonResult.ok();
    }

    @GetMapping("/ceshiasync")
    @ApiOperation("异步测试")
    public JsonResult ceshiasync() throws ExecutionException, InterruptedException {
        //ceshiTableService.completableFutureMethod();
        asyncDemoService.moreCompletableFutureMethod();
        return JsonResult.ok();
    }

    /**
     * mybatis测试
     *
     * @return {@link JsonResult }
     * @author dinghao
     * @date 2024/04/07
     */

    @GetMapping("/getList")
    @ApiOperation("mybatis测试")
    public JsonResult getList() {
        List<CeshiTable> ceshiTables = ceshiTableService.mybatisplusSelectList();
        return JsonResult.ok(ceshiTables);
    }

    @GetMapping("/getPageList")
    @ApiOperation("mybatis分页测试")
    public JsonResult getPageList() {
        Page<CeshiTable> pageList = ceshiTableService.getPageList(1, 5);
        return JsonResult.ok(pageList);
    }

    @GetMapping("/plusTest")
    @ApiOperation("mybatisService测试")
    public JsonResult plusTest() {
        CeshiTable ceshiTable = new CeshiTable();
        ceshiTable.setName("serviceName");
        ceshiTable.setAge(5L);
        //新增单条
        ceshiTableService.save(ceshiTable);
        List<CeshiTable> ceshiTables = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            CeshiTable ceshiTableNew = new CeshiTable();
            ceshiTableNew.setName("serviceName");
            Long age = (long) i;
            ceshiTableNew.setAge(age);
            ceshiTables.add(ceshiTableNew);
        }
        //批量新增
        ceshiTableService.saveBatch(ceshiTables);
        CeshiTable suCeshi = new CeshiTable();
        suCeshi.setId(9);
        suCeshi.setAge(26L);
        suCeshi.setName("hahha");
        //新增或修改单条，判断ID是否存在，如果ID不存在执行新增，如果ID存在先执行查询语句，查询结果为空新增，否则修改
        ceshiTableService.saveOrUpdate(suCeshi);
        //批量新增或修改，判断ID是否存在，如果ID不存在执行新增，如果ID存在先执行查询语句，查询结果为空新增，否则修改
        ceshiTableService.saveOrUpdateBatch(ceshiTables);
        //根据Id删除
        boolean remove = ceshiTableService.removeById(15L);
        LambdaQueryWrapper<CeshiTable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.setEntity(suCeshi);
        //根据实例删除
        boolean remove1 = ceshiTableService.remove(lambdaQueryWrapper);
        Map<String,Object> eMap = new HashMap<>();
        eMap.put("age",5L);
        eMap.put("id",17);
        //根据map删除
        ceshiTableService.removeByMap(eMap);
        return JsonResult.ok();
    }


}
